home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / mfcdual.cpp2 < prev    next >
Encoding:
Text File  |  2001-10-16  |  3.6 KB  |  114 lines

  1. // mfcdual.cpp: Helpful functions for adding dual interface support to
  2. //                MFC applications
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1996 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15.  
  16. #include <afxpriv.h>
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // DualHandleException
  26.  
  27. HRESULT DualHandleException(REFIID riidSource, const CException* pAnyException)
  28. {
  29.     USES_CONVERSION;
  30.  
  31.     ASSERT_VALID(pAnyException);
  32.  
  33.     TRACE0("DualHandleException called\n");
  34.  
  35.     // Set ErrInfo object so that VTLB binding container
  36.     // applications can get rich error information.
  37.     ICreateErrorInfo* pcerrinfo;
  38.     HRESULT hr = CreateErrorInfo(&pcerrinfo);
  39.     if (SUCCEEDED(hr))
  40.     {
  41.         TCHAR   szDescription[256];
  42.         LPCTSTR pszDescription = szDescription;
  43.         GUID    guid = GUID_NULL;
  44.         DWORD   dwHelpContext = 0;
  45.         BSTR    bstrHelpFile = NULL;
  46.         BSTR    bstrSource = NULL;
  47.         if (pAnyException->IsKindOf(RUNTIME_CLASS(COleDispatchException)))
  48.         {
  49.             // specific IDispatch style exception
  50.             COleDispatchException* e = (COleDispatchException*)pAnyException;
  51.  
  52.             guid = riidSource;
  53.             hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 
  54.                               (e->m_wCode + 0x200));
  55.  
  56.             pszDescription = e->m_strDescription;
  57.             dwHelpContext = e->m_dwHelpContext;
  58.  
  59.             // propagate source and help file if present
  60.             // call ::SysAllocString directly so no further exceptions are thrown
  61.             if (!e->m_strHelpFile.IsEmpty())
  62.                 bstrHelpFile = ::SysAllocString(T2COLE(e->m_strHelpFile));
  63.             if (!e->m_strSource.IsEmpty())
  64.                 bstrSource = ::SysAllocString(T2COLE(e->m_strSource));
  65.  
  66.         }
  67.         else if (pAnyException->IsKindOf(RUNTIME_CLASS(CMemoryException)))
  68.         {
  69.             // failed memory allocation
  70.             AfxLoadString(AFX_IDP_FAILED_MEMORY_ALLOC, szDescription);
  71.             hr = E_OUTOFMEMORY;
  72.         }
  73.         else
  74.         {
  75.             // other unknown/uncommon error
  76.             AfxLoadString(AFX_IDP_INTERNAL_FAILURE, szDescription);
  77.             hr = E_UNEXPECTED;
  78.         }
  79.  
  80.         if (bstrHelpFile == NULL && dwHelpContext != 0)
  81.             bstrHelpFile = ::SysAllocString(T2COLE(AfxGetApp()->m_pszHelpFilePath));
  82.  
  83.         if (bstrSource == NULL)
  84.             bstrSource = ::SysAllocString(T2COLE(AfxGetAppName()));
  85.  
  86.         // Set up ErrInfo object
  87.         pcerrinfo->SetGUID(guid);
  88.         pcerrinfo->SetDescription(::SysAllocString(T2COLE(pszDescription)));
  89.         pcerrinfo->SetHelpContext(dwHelpContext);
  90.         pcerrinfo->SetHelpFile(bstrHelpFile);
  91.         pcerrinfo->SetSource(bstrSource);
  92.  
  93.         TRACE("\tSource = %ws\n", bstrSource);
  94.         TRACE("\tDescription = %s\n", pszDescription);
  95.         TRACE("\tHelpContext = %lx\n", dwHelpContext);
  96.         TRACE("\tHelpFile = %ws\n", bstrHelpFile);
  97.  
  98.         // Set the ErrInfo object for the current thread
  99.         IErrorInfo* perrinfo;
  100.         if (SUCCEEDED(pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID*)&perrinfo)))
  101.         {
  102.             SetErrorInfo(0, perrinfo);
  103.             perrinfo->Release();
  104.         }
  105.  
  106.         pcerrinfo->Release();
  107.     }
  108.  
  109.     TRACE("DualHandleException returning HRESULT %lx\n", hr);
  110.     
  111.     return hr;
  112. }
  113.  
  114.